package com.j180.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.EmpBean;
import com.j180.bean.UserBean;
import com.j180.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDate;
import java.util.Base64;

@Controller
@RequestMapping("/one")
@SessionAttributes("user")
public class HelloController {

    @Autowired
    private IEmpService iEmpService;
    @Autowired
    private ObjectMapper objectMapper;

    public void setiEmpService(IEmpService iEmpService) {
        this.iEmpService = iEmpService;
    }

    @RequestMapping(value = "/test" )
    public String test1(Model model) {

        //EmpBean empBean = iEmpService.showEmp();

       // model.addAttribute("emp", empBean);

        return "hello";
    }

    @RequestMapping("/test2")
    public String test2(@RequestParam(value = "username", required = true) String name) {
        System.out.println(name);

        return "hello";
    }

    /**
     * @RequestBody 请求体   只能用于post(**)
     * 它是用来获得请求体的内容  -->获得表单中的参数
     */
    @PostMapping("/test3")
    public String test3(@RequestBody() String requestBody) {

        System.out.println(requestBody);

        try {
            //解码
            System.out.println(URLDecoder.decode(requestBody, "UTF-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "hello";
    }

    /**
     * @RequestHeader()  获得请求头的信息
     * name = "User-Agent" 获得请求头中的某个值
     */
    @RequestMapping("/test4")
    public String test4(@RequestHeader(name="Cookie")String info){

        System.out.println(info);

        return "hello";
    }

    /**
     * CookieValue("") 获得cookie的值
     *  通过键来获得值  JSESSIONID 就是键
     */
    @RequestMapping("/test5")
    public String test5(@CookieValue("JSESSIONID") String sid){
        System.out.println(sid);
        return "hello";
    }

    /**
     * @ModelAttribute 是spring4.3以后出现的新的注解
     * 作用： 用来弥补表单中没有相应的值来修改属性值的时
     *
     * @ModelAttribute: 可以用在方法上 ； 表示该方法会在目标方法执行之前执行 需要返回对象
     *                  也可以用在属性上 必须和model一起使用   方法不需要返回了
     */
    @RequestMapping("/test6")
    public String  test6(@ModelAttribute("user") UserBean userBean) {

        System.out.println("目标方法被执行了");
        System.out.println(userBean);
        return "hello";
    }


   /* @ModelAttribute
    public UserBean  addUserBefore(){
        System.out.println("ModelAttribute注解的方法被执行了");

        UserBean userBean = new UserBean(1,"old",LocalDate.now());

         return userBean;

    }*/

    @ModelAttribute
    public void  addUserBefore(Model model){
        System.out.println("ModelAttribute注解的方法被执行了");

        UserBean userBean = new UserBean(1,"old",LocalDate.now());

        model.addAttribute("user",userBean);
    }




}
